/**
 * Copyright (c) 2009 Teoti Graphix, LLC See:
 *     http://tkarchitecture.teotigraphix.org
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
 * @author Michael Schmalle mschmalle@teotigraphix.com
 */
package test.suite.dxframework.patterns.behavior
{

import flash.events.Event;

import mx.events.FlexEvent;

import org.flexunit.Assert;
import org.flexunit.async.Async;
import org.fluint.uiImpersonation.UIImpersonator;
import org.teotigraphix.dx.core.dx_internal;
import org.teotigraphix.dx.interfaces.IComponentModel;
import org.teotigraphix.dx.patterns.manager.BehaviorManager;
import org.teotigraphix.dx.patterns.manager.ModelManager;
import org.teotigraphix.dx.patterns.manager.StyleBindingManager;
import org.teotigraphix.dx.patterns.manager.meta.ClassMetaData;
import org.teotigraphix.dx.patterns.manager.meta.ComponentAliasInfo;
import org.teotigraphix.dx.patterns.manager.meta.ComponentHandlerInfo;
import org.teotigraphix.dx.patterns.manager.meta.ModelAliasInfo;
import org.teotigraphix.dx.patterns.manager.meta.SkinHandlerInfo;
import org.teotigraphix.dx.patterns.manager.meta.SkinPartAliasInfo;
import org.teotigraphix.dx.patterns.manager.meta.SkinPartHandlerInfo;

import test.suite.dxframework.support.behavior.TestBehavior;
import test.suite.dxframework.support.component.TestComponent;
import test.suite.dxframework.support.model.ITestComponentModel;
import test.suite.dxframework.support.model.TestComponentModel;
import test.suite.dxframework.support.skin.TestComponentSkin;


/**
 * @author Michael Schmalle
 * @date 07-03-09
 */
public class TC_ComponentBehavior
{
	protected var component:TestComponent;
	protected var behavior:TestBehavior;
	protected var model:TestComponentModel;
	
	
	[Before(ui,async)]
	public function setUp():void
	{
		component = new TestComponent();
		behavior = new TestBehavior();
		model = new TestComponentModel();
		
		Async.proceedOnEvent(this, component, FlexEvent.CREATION_COMPLETE);
		UIImpersonator.addChild( component );
	}
	
	[After]
	public function tearDown():void
	{
		UIImpersonator.removeChild( component );
		component = null;
		behavior = null;
		
		BehaviorManager.dx_internal::_impl = null;
		ModelManager.dx_internal::_impl = null;
	}
	
	//--------------------------------------------------------------------------
	//
	//  Public Property Tests :: Methods
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	//  className
	//----------------------------------
	
	[Test]
	public function test_get_className():void
	{
		Assert.assertEquals( 
			"test.suite.dxframework.support.behavior.TestBehavior",
			behavior.className ); 
	}
	
	//----------------------------------
	//  classMetaData
	//----------------------------------
	
	[Test]
	/**
	 * Tests whether the classMetaData was populated correctly.
	 */
	public function test_get_classMetaData():void
	{
		var data:ClassMetaData = behavior.dx_internal::getClassMetaData();
		
		// var aliases:Vector.<ComponentAliasInfo> = data.componentAliases;
		// test [ComponentAlias]
		var alias:ComponentAliasInfo = ComponentAliasInfo(data.componentAliases[0]);
		Assert.assertEquals( "myComponentAlias", alias.id );
		Assert.assertEquals( "mx.core::UIComponent", alias.type );
		
		var malias:ModelAliasInfo = data.getModelAliasByID("myModelAlias");
		Assert.assertEquals( "myModelAlias", malias.id );
		Assert.assertEquals( "test.suite.dxframework.support.model::ITestComponentModel", malias.type );
		
		// test [SkinPartAlias]
		var part:SkinPartAliasInfo = SkinPartAliasInfo(data.skinPartAliases[0]);
		Assert.assertEquals( "mySkinPart", part.id );
		// TODO required
		Assert.assertEquals( "mx.controls::TextInput", part.type );
		
		var cHandler:ComponentHandlerInfo = ComponentHandlerInfo(data.componentHandlers[0]);
		Assert.assertEquals( "component", cHandler.dispatcher );
		Assert.assertEquals( "myComponentPropertyChanged", cHandler.event );
		Assert.assertEquals( "componentHandler", cHandler.handler );
		
		var sHandler:SkinHandlerInfo = SkinHandlerInfo(data.skinHandlers[0]);
		Assert.assertEquals( "component.skin", sHandler.dispatcher );
		Assert.assertEquals( "mySkinPropertyChanged", sHandler.event );
		Assert.assertEquals( "skinHandler", sHandler.handler );
		
		var pHandler:SkinPartHandlerInfo = SkinPartHandlerInfo(data.skinPartHandlers[0]);
		Assert.assertEquals( "mySkinPart", pHandler.dispatcher );
		Assert.assertEquals( "mySkinPartPropertyChanged", pHandler.event );
		Assert.assertEquals( "skinPartHandler", pHandler.handler );		
	}
	
	[Test]
	public function test_set_component():void
	{
		BehaviorManager.registerBehavior( component, TestBehavior, behavior );
		
		Assert.assertTrue( "behavior.activateFlag", behavior.activateFlag );
		
		// test the the component has been set
		Assert.assertNotNull( "behavior.component", behavior.component );
		Assert.assertStrictlyEquals( "behavior.component", behavior.component, component );
		
		// test for the [ComponentAlias]
		Assert.assertStrictlyEquals( "behavior.myComponentAlias", behavior.myComponentAlias, component );

				
		// test for the [SkinPartAlias]
		Assert.assertNotNull( "behavior.mySkinPart", behavior.mySkinPart );
	}
	
	[Test]
	public function test_call_activate():void
	{
		Assert.assertFalse( "behavior.activateFlag", behavior.activateFlag );
		BehaviorManager.registerBehavior( component, TestBehavior, behavior );
		Assert.assertTrue( "behavior.activateFlag", behavior.activateFlag );
	}
	
	[Test]
	public function test_call_deactivate():void
	{
		Assert.assertFalse( "behavior.activateFlag", behavior.activateFlag );
		Assert.assertFalse( "behavior.deactivateFlag", behavior.deactivateFlag );
		BehaviorManager.registerBehavior( component, TestBehavior, behavior );
		Assert.assertTrue( "behavior.activateFlag", behavior.activateFlag );
		BehaviorManager.removeBehavior( component, TestBehavior );
		Assert.assertTrue( "behavior.deactivateFlag", behavior.deactivateFlag );
	}
	
	[Test]
	public function test_call_partAdded():void
	{
		Assert.assertFalse( "behavior.mySkinPartAddedFlag", behavior.mySkinPartAddedFlag );
		BehaviorManager.registerBehavior( component, TestBehavior, behavior );
		Assert.assertTrue( "behavior.mySkinPartAddedFlag", behavior.mySkinPartAddedFlag );
	}
	
	[Test]
	public function test_call_partRemoved():void
	{
		Assert.assertFalse( "behavior.mySkinPartAddedFlag", behavior.mySkinPartAddedFlag );
		BehaviorManager.registerBehavior( component, TestBehavior, behavior );
		Assert.assertTrue( "behavior.mySkinPartAddedFlag", behavior.mySkinPartAddedFlag );
		BehaviorManager.removeBehavior( component, TestBehavior );
		Assert.assertTrue( "behavior.mySkinPartRemovedFlag", behavior.mySkinPartRemovedFlag );
	}
	
	[Test]
	public function test_call_behaviorAdded():void
	{
		Assert.assertFalse( "behavior.behaviorAddedSelfFlag", behavior.behaviorAddedSelfFlag );
		BehaviorManager.registerBehavior( component, TestBehavior, behavior );
		Assert.assertTrue( "behavior.behaviorAddedSelfFlag", behavior.behaviorAddedSelfFlag );
	}
	
	[Test]
	public function test_call_behaviorRemoved():void
	{
		BehaviorManager.registerBehavior( component, TestBehavior, behavior );
		Assert.assertTrue( "behavior.behaviorAddedSelfFlag", behavior.behaviorAddedSelfFlag );
		Assert.assertFalse( "behavior.behaviorRemovedSelfFlag", behavior.behaviorRemovedSelfFlag );
		BehaviorManager.removeBehavior( component, TestBehavior );
		Assert.assertTrue( "behavior.behaviorRemovedSelfFlag", behavior.behaviorRemovedSelfFlag );
	}	
	
	[Test]
	public function test_update_StyleBinding():void
	{
		BehaviorManager.registerBehavior( component, TestBehavior, behavior );
		
		Assert.assertEquals( "", "originalBindingValue", behavior.myStyleBinding );
		
		component.setStyle( "myStyleBinding", "newValue" );
		// this mimics the ToolKitComponent.styleChanged()
		StyleBindingManager.updateStyle( "myStyleBinding", behavior, component );


		StyleBindingManager.updateStyle( "myStyleBinding", component.coreSkin, component );
		
		Assert.assertEquals( "", "newValue", behavior.myStyleBinding );
		Assert.assertEquals( "", "newValue", TestComponentSkin( component.coreSkin ).myStyleBinding );
// FIXME Took out explicit skinpart setting in ComponentBehavior
// need to figure this out for halo components
//		Assert.assertEquals( "", "newValue", component.mySkinPart.text );
	}
	
	[Test]
	public function test_component_handler():void
	{
		BehaviorManager.registerBehavior( component, TestBehavior, behavior );
		
		component.dispatchEvent( new Event( "myComponentPropertyChanged" ) );
		Assert.assertTrue( "behavior.componentFlag", behavior.componentFlag ); 
	}
	
	[Test]
	public function test_skin_handler():void
	{
		BehaviorManager.registerBehavior( component, TestBehavior, behavior );
		
		component.coreSkin.dispatchEvent( new Event( "mySkinPropertyChanged" ) );
		Assert.assertTrue( "behavior.skinFlag", behavior.skinFlag ); 
	}	
	
	[Test]
	public function test_skinpart_handler():void
	{
		BehaviorManager.registerBehavior( component, TestBehavior, behavior );
// FIXME Took out explicit skinpart setting in ComponentBehavior
// need to figure this out for halo components
//		component.mySkinPart.dispatchEvent( new Event( "mySkinPartPropertyChanged" ) );
//		Assert.assertTrue( "behavior.skinPartFlag", behavior.skinPartFlag ); 
	}
	
	[Test]
	public function test_model_handler_propertyChange():void
	{
		BehaviorManager.registerBehavior( component, TestBehavior, behavior );
		component.modelLayer.registerModel( ITestComponentModel, model );
		model.modelProperty = "testValue";
		
		Assert.assertTrue( "behavior.modelFlag", behavior.modelFlag );
		Assert.assertEquals( "behavior.newModelProperty", model.modelProperty,  behavior.newModelProperty );
	}
	
	[Test]
	public function test_model_handler_testModelPropertyChange():void
	{
		BehaviorManager.registerBehavior( component, TestBehavior, behavior );
		component.modelLayer.registerModel( ITestComponentModel, model );
		model.testModelProperty = "testValue";
		
		Assert.assertTrue( "behavior.modelFlag", behavior.modelFlag );
		Assert.assertEquals( "behavior.newModelProperty", model.testModelProperty,  behavior.newModelProperty );
	}	
	
	[Test]
	public function test_call_addOrRemoveModelAliases():void
	{
		var premodel:ITestComponentModel = new TestComponentModel();
		component.modelLayer.registerModel(ITestComponentModel, model);
		BehaviorManager.registerBehavior( component, TestBehavior, behavior );
		
		Assert.assertNotNull( "behavior.myPreModelAlias", behavior.myPreModelAlias );
		Assert.assertTrue( "!IComponentModel", (behavior.myPreModelAlias is IComponentModel) );
		Assert.assertTrue( "!ITestComponentModel", (behavior.myPreModelAlias is ITestComponentModel) );
	}
	
	[Test]
	public function test_call_modelAdded():void
	{
		BehaviorManager.registerBehavior( component, TestBehavior, behavior );
		ModelManager.registerModel( component, ITestComponentModel, model );
		
		Assert.assertTrue( "behavior.modelAddedFlag", behavior.modelAddedFlag );
		Assert.assertNotNull( "behavior.myModelAlias", behavior.myModelAlias );
		Assert.assertTrue( "!IComponentModel", (behavior.myModelAlias is IComponentModel) );
		Assert.assertTrue( "!ITestComponentModel", (behavior.myModelAlias is ITestComponentModel) );
	}
	
	[Test]
	public function test_call_modelRemoved():void
	{
		BehaviorManager.registerBehavior( component, TestBehavior, behavior );
		ModelManager.registerModel( component, ITestComponentModel, model );
		var omodel:IComponentModel = ModelManager.removeModel( component, ITestComponentModel );
		
		Assert.assertTrue( "behavior.modelRemovedFlag", behavior.modelRemovedFlag );
		Assert.assertNull( "behavior.myModelAlias", behavior.myModelAlias );
		Assert.assertNotNull( "behavior.omodel", omodel );
	}
}
}